package com.StarStudio.Controller;

import com.StarStudio.Service.GoodsStockService;
import com.StarStudio.Utils.QueryPageUtils;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.Entity.*;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/goods_stock")
@Api(tags = "商品库存接口")
public class GoodsStockController {

    @Autowired
    GoodsStockService goodsStockService;

    /**
     * 商品库存分页方法
     * @param goodsStock 商品库存对象
     * @return 分页结果对象 PageResult
     */
    @GetMapping("/page")
    @ApiOperation("商品库存分页接口 -> 支持条件检索，检索条件封装在商品库存个体对象GoodsStock中。如需分页，必传pageNum和pageSize")
    public PageResult Page(GoodsStock goodsStock){
        QueryPageUtils.startPage();
        PageInfo pageInfo;
        List<GoodsStock> goodsStockList;
        long allRecordNumber;
        try {
            goodsStockList = goodsStockService.findGoodsStockList(goodsStock);
            SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
            if (sysUser.isAdmin()){
                allRecordNumber = goodsStockService.findAllRecordNumber();
            }else {
                allRecordNumber = goodsStockService.findStoreAllRecordNumber(sysUser.getUserId());
            }
            pageInfo = new PageInfo<>(goodsStockList);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(HttpStatusCode.ERROR, MessageConstant.QUERY_PAGE_FAIL);
        }
        return new PageResult(HttpStatusCode.SUCCESS,MessageConstant.QUERY_PAGE_SUCCESS, pageInfo.getTotal(), goodsStockList, allRecordNumber);
    }

    /**
     * 导出商品库存详情信息到 Excel表格
     * @param response http响应
     * @param goodsStock 商品库存对象
     */
    @Log(title = "商品库存详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("商品库存Excel导出接口 -> 支持条件检索，检索条件封装在商品库存个体对象GoodsStock中")
    public void export(HttpServletResponse response, GoodsStock goodsStock) {
        List<GoodsStock> list = goodsStockService.findGoodsStockList(goodsStock);
        ExcelUtil<GoodsStock> util = new ExcelUtil<>(GoodsStock.class);
        util.exportExcel(response, list, "商品库存数据");
    }
}
